<?php

namespace app\admin\controller;

use think\Db;

class SystemConfig extends Base
{
    public function editSystemConfig()
    {
        $data = [];
        $data['name'] = input('name');
        $data['serverFeeExplain'] = input('serverFeeExplain');
        $data['examExplain'] = input('examExplain');
        $data['publishExplain'] = input('publishExplain');
        $data['faceValue'] = input('faceValue');
        $data['realValue'] = input('realValue');
        $data['dataValue'] = input('dataValue');
        $data['safeValue'] = input('safeValue');
        $data['safeTestValue'] = input('safeTestValue');
        $data['friendInviteValue'] = input('friendInviteValue');
        $data['firstInviteReward'] = input('firstInviteReward');
        $data['secondInviteReward'] = input('secondInviteReward');
        $data['thirdInviteReward'] = input('thirdInviteReward');
        $data['secondInviteValue'] = input('secondInviteValue');
        $data['thirdInviteValue'] = input('thirdInviteValue');
        $data['lightenFee'] = input('lightenFee');
        $data['vipMoney'] = input('vipMoney');
        $data['hotline'] = input('hotline');
        input('cashPercent') ? $data['cashPercent'] = input('cashPercent') : true;
//        input('shareReward')? $data['shareReward']=input('shareReward'):$this->err('缺少分享金比率！');
        input('yearVip')? $data['yearVip']=input('yearVip'):$this->err('缺少年卡金额！');
        input('partnerInviteReward')? $data['partnerInviteReward']=input('partnerInviteReward'):$this->err('缺少合伙人比率！');
        input('content')? $data['content']=input('content'):$this->err('缺少文本内容！');
        input('videoSrc')? $data['videoSrc']=input('videoSrc'):$this->err('缺少视频！');
        $data['videoPicSrc'] = $data['videoSrc'] . '?vframe/jpg/offset/5/w/640/h/360';
        $result = $this->validate($data, 'SystemConfig');
        if ($result !== true) {
            $this->err($result);
        }
        if (input('id')) {
            $data['id'] = input('id');
            $rep = model('system_config')->editData($data, $data['id']);
        } else {
            $data['createTime'] = createTime();
            $rep = model('system_config')->editData($data);
        }

        $rep ? $this->suc($rep) : $this->err('修改失败');

    }

    public function enableConfig()
    {
        $data = [];
        input('id') ? $data['id'] = input('id') : $this->err('缺少主键');
        $data['status'] = 1;
        $where = [];
        $where['id'] = ['<>', $data['id']];

        // 启动事务
        Db::startTrans();
        try {
            model('system_config')->editData($data, $data['id']);
            $res = model('system_config')->where($where)->update(['status' => 0]);
            // 提交事务
            Db::commit();
            $this->suc('操作成功！');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $this->err('操作失败！');
        }

    }

    public function getConfigList()
    {
        $where = [];
        input('name') ? $where['name'] = ['like', '%' . input('name') . '%'] : true;
        input('status') ? $where['status'] = input('status') : true;

        $res = model('system_config')->getListByPage($where, 10, '', 'status desc, updateTime desc');
        $res ? $this->suc($res) : $this->err('获取列表失败');
    }

    public function getConfigDetail()
    {
        $where = [];
        input('id') ? $where['id'] = input('id') : $this->err('缺少主键');

        $res = model('system_config')->where($where)->find();
        $res ? $this->suc($res) : $this->err('获取系统配置详情失败');
    }

    /**
     * 获取七牛上传token
     */
    public function getUpToken()
    {
        // 用于签名的公钥和私钥
        $accessKey = config('app.qiniu.ACCESSKEY');
        $secretKey = config('app.qiniu.SECRETKEY');
        // 初始化签权对象
        $auth = new Auth($accessKey, $secretKey);

        $bucket = config('app.qiniu.BUCKET');

        // 生成上传Token
        $token = $auth->uploadToken($bucket);
        $time = intval(time());
        $this->suc(['uptoken' => $token, 'time' => $time]);

    }


}
